#pragma runtime_checks( "scu", off )

#include "../[msvs projects]/GatewayLoginManager/MyLoginmanager.h"

#include<fstream>
#include<map>
#include<string>
#include "relay.h"

using namespace std;

//#define SLEEPING

//class MyLoginManager : public BAM::cLoginManager
//{
//public:
//        MyLoginManager()
//        {
//        }
//
//        BAM::cPacket getPassword(const BAM::cPacket &login)
//        {
///*                BAM::cPacket P = login;
//                P.insertTail((const BAM::byte*)"", 1);
//                map<string, string>::const_iterator it = vMap.find((const char*)P.getData());
//                if(it == vMap.end())
//                        throw BAM::EXC_Login;*/
//			return BAM::cPacket((BAM::byte*)"123", 3);
//        }
//};

#ifndef _WIN32
#define stricmp strcasecmp
#endif

int main(int argc, char** argv)
{
	MyLoginManager lm;
	//lm.getPassword(BAM::cPacket((BAM::byte*)"petya", 5));

		cout << "		RELAY STARTED" << endl;
        if(argc != 2)
        {
                cout << "Usage: " << argv[0] << " listening-adderss" << endl;
				
				#ifdef SLEEPING
				Sleep(5000);
				#endif

                return 1;
        }
		try
		{
	        MyLoginManager *lm = new MyLoginManager;
			BAM::cPRPRelay* fwd = new BAM::cPRPRelay(argv[1], lm, false);
			BAM::cRoutersHolder *hld = new BAM::cRoutersHolder;
			fwd->start();
			hld->add(fwd);
			
			hld->run();
			
/*			while (true)
			{
				fwd->onReadable(hld);
				fwd->onIdle(hld);
				Sleep(5);
			}
*/
			delete fwd;
			cout << "Terminated normally" << endl;
			
			#ifdef SLEEPING
			Sleep(5000);
			#endif
			
			return 0;
		}
		catch (const BAM::SocketException &e)
		{
			cout << e.what();
			
			#ifdef SLEEPING
			Sleep(5000);
			#endif
			
			return 1;
		}
		catch (const std::cException &e)
		{
			cout << e.what();
			
			#ifdef SLEEPING
			Sleep(5000);
			#endif

			return 1;
		}

		catch(...)
		{
			//printf("Can not find file 'passwd'\n");
			cout << "Generic exception" << endl;
			
			#ifdef SLEEPING
			Sleep(5000);
			#endif

			return 1;
		}
}




//#include<fstream>
//#include<map>
//#include<string>
//#include "relay.h"
//
////#include "C:\\Documents and Settings\\sfaktorovich\\My Documents\\remoteconsole2\\vld.h"
//class cFileLoginManager : public BAM::cLoginManager
//{
//private:
//        map<string, string> vMap;
//
//public:
//        cFileLoginManager(const char* filename)
//        {
//                ifstream f(filename);
//                if(!f)
//                        throw cException("Unable to open password file");
//                string login, pass;
//                while(!f.eof())
//                {
//                        f >> login;
//                        if(f.eof())
//                                throw cException("Wrong password file format");
//                        f >> pass;
//                        vMap.insert(pair<string, string>(login, pass));
//                }
//        }
//
//        BAM::cPacket getPassword(const BAM::cPacket &login)
//        {
//                BAM::cPacket P = login;
//                P.insertTail((const BAM::byte*)"", 1);
//                map<string, string>::const_iterator it = vMap.find((const char*)P.getData());
//                if(it == vMap.end())
//                        throw BAM::EXC_Login;
//                return BAM::cPacket((const BAM::byte*)(it->second.c_str()), strlen(it->second.c_str()));
//        }
//};
//
//#ifndef _WIN32
//#define stricmp strcasecmp
//#endif
//
//int main(int argc, char** argv)
//{
//		cout << "		RELAY STARTED" << endl;
//        if(argc != 2)
//        {
//                cout << "Usage: " << argv[0] << " listening-adderss" << endl;
//                return 1;
//        }
//		try
//		{
//	        cFileLoginManager lm("passwd");
//			BAM::cPRPRelay* fwd = new BAM::cPRPRelay(argv[1], &lm, false);
//			BAM::cRoutersHolder hld;
//			fwd->start();
//			hld.add(fwd);
//			
//			hld.run();
//			
///*			while (true)
//			{
//				fwd->onReadable(hld);
//				fwd->onIdle(hld);
//				Sleep(5);
//			}
//*/
//			delete fwd;
//			return 0;
//		}
//		catch(...)
//		{
//			printf("Can not find file 'passwd'\n");
//			return 1;
//		}
//}
